#ifndef __H_GAMEBASE_H__
#define __H_GAMEBASE_H__

#include "Packet.h"
#include "ClientHandler.h"
#include "Logic.h"
#include "eventitem.h"

class ClientBase;
class TcpListener;
class UdpListener;

class CGameBase
{
public:
    CGameBase()
	{
		_heatbeat_check = false;
	}
    virtual ~CGameBase() {}	
	virtual bool InitGame() = 0;
public: //callback
	virtual int OnClose(CClientHandler* pHandler) = 0;
	virtual int ProcessPacket(CClientHandler * pHandler, NETInputPacket* inputPkg) = 0;

	virtual int OnBackClose(ClientBase* pBackClient) = 0;
	virtual int OnBackConnected(ClientBase* pBackClient) = 0;
	virtual int ProccessBackPacket(ClientBase* pBackClient, NETInputPacket* inputPkg) = 0;
	
	// udp recv data,  inet_ntoa(peer_addr.sin_addr),  peer_addr.sin_port)
	virtual int ProccessUpdPacket(char* raw_data, int data_len, struct sockaddr_in& peer_addr){ return 0; }

	virtual int OnEventCallBack(stEventItem* pitem) = 0;
public: //method
	int TcpListen(const char*ip, uint16_t port);
	int UdpListen(const char*ip, uint16_t port);
	void EnableHeatBeatCheck(){ _heatbeat_check = true; }
	void DisableHeatBeatCheck(){ _heatbeat_check = false; }
	inline bool IsHeatBeatCheck(){ return _heatbeat_check; }
	CClientHandler* GetClientHandler(int handler_id);
public: 
	virtual CClientHandler* CreateHandler(int netfd, struct sockaddr_in* peer);
	int AcceptNewHandler(CClientHandler* pHandler);
public:	
	bool _heatbeat_check;
	CCHandlerMap m_handlerList;
};

extern void InitDaemonLog();

class Logger :public BYTimerOject
{
public:

	enum TIME_ID
	{
		NODE_TIMERID_LOGFILE = 0x101,
	};

	Logger(){};
	virtual ~Logger(){};
	int OnBYTimeOut(int Timerid)
	{
		switch (Timerid)
		{
		case NODE_TIMERID_LOGFILE:
			this->StopTimer(NODE_TIMERID_LOGFILE);
			this->StartTimer(NODE_TIMERID_LOGFILE, CLogic::GetCurrDayLeastTime());
			InitDaemonLog();
			break;
		default:
			break;
		}
		return 0;
	}
};

#endif

